package com.ccb.sc.common.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccb.sc.common.bind.vo.KeyValueVo;
import com.ccb.sc.common.modules.common.entity.LogisticsCompany;
import com.ccb.sc.common.modules.common.mapper.LogisticsCompanyMapper;
import com.ccb.sc.common.modules.common.service.ILogisticsCompanyService;
import com.google.common.collect.Lists;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 物流公司 服务实现类
 * </p>
 *
 * @author ken
 * @since 2020-11-12
 */
@Service
@CacheConfig(cacheNames = "com:logistics-company")
public class LogisticsCompanyServiceImpl extends ServiceImpl<LogisticsCompanyMapper, LogisticsCompany> implements ILogisticsCompanyService {

    @Override
    @Cacheable
    public List<KeyValueVo> findList() {
        List<LogisticsCompany> list = this.list();
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        return list.stream().map(item -> {
            KeyValueVo vo = new KeyValueVo();
            vo.setKey(item.getCode());
            vo.setValue(item.getName());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    @Cacheable
    public Map<String, String> findMap() {
        List<LogisticsCompany> list = this.list();
        Map<String, String> map = new HashMap<>(list.size());
        if (CollectionUtils.isEmpty(list)) {
            return map;
        }
        return list.stream().collect(Collectors.toMap(LogisticsCompany::getCode, LogisticsCompany::getName));
    }

    @Override
    public LogisticsCompany findByCode(String code) {
        return this.getOne(new QueryWrapper<LogisticsCompany>().lambda().eq(LogisticsCompany::getCode, code));
    }
}
